/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    spi.c
  * @brief   This file provides code for the configuration
  *          of the SPI instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "spi.h"

/* USER CODE BEGIN 0 */
#include "lcd.h"
#include "w25flash.h"

#include <string.h>
#include <stdio.h>



/* USER CODE END 0 */

SPI_HandleTypeDef hspi1;

/* SPI1 init function */
void MX_SPI1_Init(void)
{

  /* USER CODE BEGIN SPI1_Init 0 */

  /* USER CODE END SPI1_Init 0 */

  /* USER CODE BEGIN SPI1_Init 1 */

  /* USER CODE END SPI1_Init 1 */
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI1_Init 2 */

  /* USER CODE END SPI1_Init 2 */

}

void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(spiHandle->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspInit 0 */

  /* USER CODE END SPI1_MspInit 0 */
    /* SPI1 clock enable */
    __HAL_RCC_SPI1_CLK_ENABLE();

    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**SPI1 GPIO Configuration
    PB3     ------> SPI1_SCK
    PB4     ------> SPI1_MISO
    PB5     ------> SPI1_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* USER CODE BEGIN SPI1_MspInit 1 */

  /* USER CODE END SPI1_MspInit 1 */
  }
}

void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
{

  if(spiHandle->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspDeInit 0 */

  /* USER CODE END SPI1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_SPI1_CLK_DISABLE();

    /**SPI1 GPIO Configuration
    PB3     ------> SPI1_SCK
    PB4     ------> SPI1_MISO
    PB5     ------> SPI1_MOSI
    */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5);

  /* USER CODE BEGIN SPI1_MspDeInit 1 */

  /* USER CODE END SPI1_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */
void Falsh_testStatus(){
	uint16_t devID = Flash_ReadID();
	char tempstrdevID[30];
	sprintf(tempstrdevID,"Device ID = 0x%04X\r\n",devID);
	lcd_show_str(10, 2 * 30  , 16, tempstrdevID,RED);
	switch(devID)
	{
	case 0xEF17:
		lcd_show_str(10, 3 * 30  , 16, "The chip is: Winbond",RED);
		break;
	case 0xC817:
		lcd_show_str(10, 3 * 30  , 16, "The chip is: GD25Q128",RED);
		break;
	case 0x1C17:
		lcd_show_str(10, 3 * 30  , 16, "The chip is: EN25Q128",RED);
		break;
	case 0xA117:
		lcd_show_str(10, 3 * 30  , 16, "The chip is: FM25Q128",RED);
		break;
	case 0x2018:
		lcd_show_str(10, 3 * 30  , 16, "The chip is: N25Q128",RED);
		break;
	case 0x2017:
		lcd_show_str(10, 3 * 30  , 16, "The chip is: XM25QH128",RED);
		break;
	default:
		lcd_show_str(10, 3 * 30  , 16, "unknown type",RED);
		break;
	}

	char bufstrSR1[30];
	sprintf(bufstrSR1,"Status Reg1 = 0x%04X\r\n",Flash_ReadSR1());
	lcd_show_str(10, 4 * 30  , 16, bufstrSR1,RED);

	char bufstrSR2[30];
	sprintf(bufstrSR2,"Status Reg2 = 0x%04X\r\n",Flash_ReadSR2());
	lcd_show_str(10, 5 * 30  , 16, bufstrSR2,RED);

}
void Flash_Testwrite(){
	uint8_t BlockNo = 0;
	uint8_t SubSectorNo = 0;
	uint8_t SubPageNo = 0;
	uint32_t memadress = 0;
	memadress = Flash_Addr_byBlockSectorPage(BlockNo,SubSectorNo,SubPageNo);

	char bufstr1[30] = "Hello From Beginning";
	Flash_WriteInPage(memadress, bufstr1, strlen(bufstr1) + 1);
	lcd_show_str(10, 10 * 30  , 16, "Write in page0: 0", RED);
	lcd_show_str(10, 11 * 30  , 16, bufstr1, RED);

	char bufstr2[30] = "Hello in Page";
	Flash_WriteInPage(memadress+ 100, bufstr2, strlen(bufstr2) + 1);
	lcd_show_str(10, 12 * 30  , 16, "Write in page0: 100", RED);
	lcd_show_str(10, 13 * 30  , 16, bufstr2, RED);

	char bufPage[FLASH_PAGE_SIZE];
	for(uint16_t i = 0;i < FLASH_PAGE_SIZE; ++i)
	{
		bufPage[i] = i;
	}
	SubPageNo = 1;
	memadress = Flash_Addr_byBlockSectorPage(BlockNo, SubSectorNo, SubPageNo);
	Flash_WriteInPage(memadress, bufPage, FLASH_PAGE_SIZE);
	lcd_show_str(10, 14 * 30  , 16, "Write in page1: 0 ~ 255", RED);
}
void Flash_Testread(){

	uint8_t BlockNo = 0;
	uint8_t SubSectorNo = 0;
	uint8_t SubPageNo = 0;
	lcd_show_str(10, 10 * 30  , 16, "Reading,waiting", RED);
	uint32_t memadress = Flash_Addr_byBlockSectorPage(BlockNo,SubSectorNo,SubPageNo);

	char bufstr[50];
	Flash_ReadBytes(memadress,bufstr,50);
	lcd_show_str(10, 10 * 30  , 16, "Read in page0: 0", RED);
	lcd_show_str(10, 11 * 30 +10, 16, bufstr, RED);

	Flash_ReadBytes(memadress +100,bufstr,50);
	lcd_show_str(10, 12 * 30  , 16, "Read in page0: 100", RED);
	lcd_show_str(10, 13 * 30 +10, 16, bufstr, RED);

	SubPageNo = 1;
	memadress = Flash_Addr_byBlockSectorPage(BlockNo,SubSectorNo,SubPageNo);

	uint8_t randDate12 = Flash_ReadOneByte(memadress + 12);
	uint8_t randDate126 = Flash_ReadOneByte(memadress + 126);
	uint8_t randDate210 = Flash_ReadOneByte(memadress + 210);
	char tempstrrandDate[30];
	sprintf(tempstrrandDate,"page1[12]=%d,[126]=%d,[210]=%d",
			randDate12,randDate126,randDate210);
	lcd_show_str(10, 14 * 30  , 16, tempstrrandDate, RED);
}
/* USER CODE END 1 */
